package example.service.impl;



import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import example.common.Result;
import example.entity.TaskList;
import example.mapper.TaskListMapper;
import example.service.ITaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 借调历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class TaskListServiceImpl implements ITaskListService
{
    @Resource
    private TaskListMapper taskListMapper;

    /**
     * 查询借调历史
     *
     * @param id 借调历史主键
     * @return 借调历史
     */
    @Override
    public TaskList selectTaskListById(Long id)
    {
        return taskListMapper.selectTaskListById(id);
    }

    /**
     * 查询借调历史列表
     *
     * @param taskList 借调历史
     * @return 借调历史
     */
    @Override
    public List<TaskList> selectTaskListList(TaskList taskList)
    {
        return taskListMapper.selectTaskListList(taskList);
    }

    /**
     * 新增借调历史
     *
     * @param taskList 借调历史
     * @return 结果
     */
    @Override
    public int insertTaskList(TaskList taskList)
    {
        return taskListMapper.insertTaskList(taskList);
    }

    /**
     * 修改借调历史
     *
     * @param taskList 借调历史
     * @return 结果
     */
    @Override
    public int updateTaskList(TaskList taskList)
    {
        return taskListMapper.updateTaskList(taskList);
    }

    /**
     * 批量删除借调历史
     *
     * @param ids 需要删除的借调历史主键
     * @return 结果
     */
    @Override
    public int deleteTaskListByIds(Long[] ids)
    {
        return taskListMapper.deleteTaskListByIds(ids);
    }

    /**
     * 删除借调历史信息
     *
     * @param id 借调历史主键
     * @return 结果
     */
    @Override
    public int deleteTaskListById(Long id)
    {
        return taskListMapper.deleteTaskListById(id);
    }


    public Result confirmTime(TaskList taskList){
        taskListMapper.confirmTime(taskList);
        return Result.ok("确认时间成功");
    }

    public void export(HttpServletResponse response, TaskList taskList){
        List<TaskList> list =taskListMapper.selectTaskListList(taskList);
        try{
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("学生表.xls","utf-8"));
            //输出excel
            EasyExcel
                    .write(response.getOutputStream(),TaskList.class)
                    .autoCloseStream(true)
                    .sheet("借调历史")
                    .doWrite(list);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            throw new RuntimeException(e);
        }
    }


}
